package com.lemon.smartsafetynew.ui.activity.ledger;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.entity.event.common.DateEvent;
import com.lemon.smartsafetynew.entity.ledger.LedgerPatrolPersonnelStatisticsVo;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerPatrolPersonnelDetailFragment;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/9/14.
 */

public class LedgerPatrolPersonnelDetailActivity extends SwipeBackActivity {

    @Bind(R.id.user_name_tv)
    TextView userNameTv;
    @Bind(R.id.department_photo)
    ImageView departmentPhoto;
    @Bind(R.id.department_name_tv)
    TextView departmentNameTv;

    private static LedgerPatrolPersonnelStatisticsVo mLedgerPatrolPersonnelStatisticsVo;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_LEDGER_PATROL_PERSONNEL_STATISTICS)) {
                mLedgerPatrolPersonnelStatisticsVo = (LedgerPatrolPersonnelStatisticsVo) bundle.getSerializable(KeyConstants.KEY_LEDGER_PATROL_PERSONNEL_STATISTICS);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle("人员任务概况");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerPatrolPersonnelDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_patrol_personnel_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initCommon();
        transactionSupportFragment(LedgerPatrolPersonnelDetailFragment.newInstance(mLedgerPatrolPersonnelStatisticsVo));

    }

    /**
     * 页面初始化
     */
    private void initCommon() {
        userNameTv.setText(CommonUtil.avoidNullString(mLedgerPatrolPersonnelStatisticsVo.getUser_name()));
        departmentNameTv.setText(CommonUtil.avoidNullString(mLedgerPatrolPersonnelStatisticsVo.getDept_name()));
    }

    private void transactionSupportFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fragment).commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        getMenuInflater().inflate(R.menu.menu_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_calendar: {
                pickCheckDate();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 修改日期
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat= new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());

                AppService.getBus().post(new DateEvent(dateStr, LedgerPatrolPersonnelDetailActivity.class.getName()));

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

}
